
package commands;

import application.Core;

/**
 *	This class implements command shutdown.
 * 
 * @author Michal Mourek
 */
public class Shutdown extends AbstractCommand{
	/**
	 * Main method of this program.
	 * It close system.
	 *
	 * @param parameters List of parameters entered by user in command line.
	 * @param arguments List of arguments entered by user in command line.
	 * @param id Process ID.
	 * @param core reference to the core
	 * @author Michal Mourek
	 */
	@Override
	public void main(String[] parameters, String[] arguments, int id, Core core) {
		if (parameters.length == 0){
			if (arguments.length == 0){
				core.shutdown();
				
			} else {
				String[] arrayString = invalidArguments().split("\n");
				for (String string : arrayString) {
					core.write(id, string);
					try {
						Thread.sleep(50);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				
			}
		} else {
			for (int i = 0; i < parameters.length; i++) {
					char parameter = parameters[i].charAt(1);
					if (parameter == 'h'){
						String[] help = help().split("\n");
						for (String string : help) {
							core.write(id, string);
							try {
								Thread.sleep(50);
							} catch (InterruptedException e) {
							// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					} else{
						String[] invPar = invalidParameters().split("\n");
						for (String string : invPar) {
							core.write(id, string);
							try {
								Thread.sleep(50);
							} catch (InterruptedException e) {
							// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
						break;
					}
				}
		}
		//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
	}

	@Override
	public String help() {
		return ("NAME: shutdown - close system \n SYNOPSYS: shutdown [OPTION] \n \n Description: \n   -h      print help \n");
		
		//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
	}
	
	/**
	 * Print warning message, when parameter is invalid.
	 *
	 * @author Michal Mourek
	 */
	public String invalidParameters(){
		return ("Parameters is not correct \n Read help shutdown - h \n");
	}
	
	/**
	 * Print warning message, when argument is invalid.
	 *
	 * @author Michal Mourek
	 */
	public String invalidArguments(){
		return ("Arguments is not correct \n Read help shutdown - h \n");
	}
}
